package com.huatai.common.httputil;

/**
 * @program: ht-system
 * @description:
 * @author: songxiude
 * @create: 2023-04-25 15:53
 **/

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import okhttp3.Authenticator;
import okhttp3.Credentials;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

public class BaseAuthenticator implements Authenticator {
	private final String userName;
	private final String password;

	public Request authenticate(Route route, Response response) throws IOException {
		String credential = Credentials.basic(this.userName, this.password, StandardCharsets.UTF_8);
		return response.request().newBuilder().header("Authorization", credential).build();
	}

	public BaseAuthenticator(final String userName, final String password) {
		this.userName = userName;
		this.password = password;
	}
}

